﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using wsn.atos;
using System.Xml;

namespace wsn.ap
{
    class ComAccessPoint : IAccessPoint//串口接入点
    {
        SerialPort com;
        IComponent component;
        
        public bool Open()            // 打开串口
        {
            try
            {
                com = new SerialPort();
                
                com.BaudRate =Convert.ToInt32( GetSerial_configuration("波特率"));

                
                com.PortName = GetSerial_configuration("端口号");
                com.DataBits = Convert.ToInt32( GetSerial_configuration("数据位"));

                Console.WriteLine(com.BaudRate);
                Console.WriteLine(com.PortName);

                com.Open();
                if (com.IsOpen == false)
                {
                    
                    return false;
                }
              
                return true;
            }
            catch(Exception e)
            {
                Console.WriteLine(e.StackTrace);
                return false;
            }
        }
        public int[] GetData()
        {
            int[] package_head = new int[5];
            package_head[1] = com.ReadByte();
            package_head[2] = com.ReadByte();
            do
            {
                package_head[0] = package_head[1];
                package_head[1] = package_head[2];
                package_head[2] = com.ReadByte();
            } while (package_head[0] != 0x37 || package_head[1] != 0x24 || package_head[2] != 0x30);
            package_head[3] = com.ReadByte();
            package_head[4] = com.ReadByte();
            int[] package_data = new int[package_head[3] * 16 + package_head[4] + 1];
            for (int k = 0; k <= (package_head[3] * 16 + package_head[4]); k++)
            {
                package_data[k] = com.ReadByte();
            }
            return package_data;

        }

        public void run()
        {
            if (com.IsOpen == false)
                return;
            while(true){
                int[] data = GetData();
                AccessPointData newData=component.execute(data);
                Service.addPointData(newData);
                Console.WriteLine("数据：{0}",newData.Sensor_ID);
            }
        }

        public void Close()
        {
            com.Close();
            if (com.IsOpen == false)
                Console.WriteLine("串口{0}关闭成功", com.PortName);
        }

        public void setConponent(IComponent c)
        {
            component = c;
        }
        public bool IsOpen()
        {
              return com.IsOpen; 
        }
        public string GetSerial_configuration(string data)//通过XML配置文件查询为data项
        {                                                   //对应的值
            XmlDocument doc = new XmlDocument();
            doc.Load("Com.xml");
            XmlNodeList comread = doc.GetElementsByTagName(data);
            return comread[0].InnerXml;
           
        }


    }
}
